home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / Libraries / MCC Utils / MCCursor.p < prev    next >
Encoding:
Text File  |  1994-05-04  |  8.6 KB  |  243 lines  |  [TEXT/PJMM]

  1. {This document is formated in monaco 9 pt                                          }
  2. {                                                                                  }
  3. {LEGAL STUFF                                                                       }
  4. {                                                                                  }
  5. {Copyright © 1994 by University of Melbourne. All Rights Reserved. This work is    }
  6. {provided "as is" and without any express or implied warranties, including,        }
  7. {without limitation, the implied warranties of merchantability and fitness         }
  8. {for a particular purpose.                                                         }
  9. {                                                                                  }
  10. {University of Melbourne is not responsible for the consequences of the use of this}
  11. {work, regardless of the cause. You may use this work in a public domain,          }
  12. {freeware, or shareware product with no restrictions, as long as you include       }
  13. {the following notice in your product's about box or splash screen:                }
  14. {  "Portions Copyright © 1994 by University of Melbourne".                         }
  15. {If you use more than 50 lines of this work, please credit the author also:        }
  16. {  "Portions by Michael Cutter"                                                    }
  17. {Public domain is defined as something that you release to the public, without     }
  18. {copyright and without restrictions on use. Freeware is a copyrighted work,        }
  19. {for which you charge no money. Shareware is a copyrighted work for which you      }
  20. {charge a fee if the user decides to keep it. If you intend to use this work       }
  21. {in a commercial product, please contact us.                                       }
  22. {                                                                                  }
  23. {                                                                                  }
  24. {OTHER STUFF                                                                       }
  25. {                                                                                  }
  26. {AUTHOR:                                                                           }
  27. { Michael Trevor Cutter                                                            }
  28. {                                                                                  }
  29. {CONTACT:                                                                          }
  30. {  Internet:                                                                       }
  31. {    mtc@arbld.unimelb.edu.au (Preferred)                                          }
  32. {  Snail Mail:                                                                     }
  33. {    Dept of Architecture & Building                                               }
  34. {    University of Melbourne                                                       }
  35. {    Parkville VIC 3052                                                            }
  36. {    AUSTRALIA                                                                     }
  37. {                                                                                  }
  38. {PERSONAL STUFF                                                                    }
  39. {  I'd really appreciate it if you'd let me know what you're using my code         }
  40. {  in, (send me email or a postcard). Please report any bugs or errors to me.      }
  41. {                                                                                  }
  42. {MODULE DESCRIPTION                                                                }
  43. {This module provides general cursor utilities, and three functions for using      }
  44. {colour animated cursors. This does not install a VBL task, instead you simply     }
  45. {call the MCNextAnimCursor function whenever you want to move it on. This is much  }
  46. {more user-friendly (IMHO) because it indicates actual processing.                 }
  47.  
  48. unit MCCursor;
  49. interface
  50.  
  51. {like Hypercard's set cursor, but supports colour cursors}
  52.     procedure MCSetCursor (resid: integer;
  53.                                     releasecursor: Boolean);
  54.  
  55. {obvious, really}
  56.     procedure MCWatchCursor;
  57.     procedure MCIBeamCursor;
  58.     procedure MCPlusCursor;
  59.     procedure MCCrossCursor;
  60.     procedure MCArrowCursor;
  61.  
  62. {To use colour animated cursors, provide an acur resource specifying the ids and}
  63. {delay in order, and colour cursor resources with the matching ids as the b&w ones}
  64. {Then, call  MCStartAnimCursor  (generally in the initialization sequence of your}
  65. {program) with the acur id.}
  66. {Then, at appropriate points in your program (busy loops etc), call  MCNextAnimCursor  }
  67. {which will animate the cursor forward one step}
  68.  
  69. {for animated cursors}
  70. {this procedure finds the acur resource specified,}
  71. {and loads it into memory - note limitation of 64 cursors, but you can change}
  72. {that by modifying the size of the array.}
  73.     procedure MCStartAnimCursor (acurresid: integer);
  74.  
  75. {this procedure checks the cursor counter, and whether enough time}
  76. {(as specified in the acur) has passed, and if so, gets the next}
  77. {cursor (colour if available) and sets it}
  78.     procedure MCNextAnimCursor;
  79.  
  80. {note - there are one or two simple functions, such as getting the current screen depth,}
  81. {which are not provided}
  82.  
  83. implementation
  84.     const
  85.         kMCCurMaxCursors = 64;
  86.     var
  87.         gMCCurrHasColour: Boolean;
  88.         gMCcurracurid: integer;
  89.         gMCcurrcurindex: integer;
  90.         gMCacurids: array[1..kMCCurMaxCursors] of integer;
  91.         gMCcurscount: integer;
  92.         MCcursLastTime: longint;
  93.         MCcursCursorsOn: boolean;
  94.         MCcursInterval: integer;
  95.  
  96.     function MCHasColourQD: Boolean;
  97.         var
  98.             myComputer: SysEnvRec;
  99.             myErr: OSErr;
  100.     begin
  101.         myErr := SysEnvirons(1, myComputer);
  102.         if ((myErr = noErr) and (myComputer.hasColorQD)) then
  103.             MCHasColourQD := true
  104.         else
  105.             MCHasColourQD := false;
  106.     end;
  107.  
  108.     procedure MCSetCursor;
  109.         var
  110.             curs: CursHandle;
  111.             ccurs: CCrsrHandle;
  112.     begin
  113.         curs := nil;
  114.         if gMCCurrHasColour then
  115.             if (GetMainDevice^^.gdPMap^^.pixelsize > 2) then
  116.                 begin
  117.                     ccurs := GetCCursor(resid);
  118.                     if (ccurs <> nil) and (ResError = noErr) then
  119.                         begin
  120.                             SetCCursor(ccurs);
  121.                             if releasecursor then
  122.                                 DisposeCCursor(ccurs);
  123.                             exit(MCSetCursor);
  124.                         end;
  125.                 end;
  126.         curs := GetCursor(resid);
  127.         if (curs <> nil) and (ResError = noErr) then
  128.             begin
  129.                 hlock(handle(curs));
  130.                 SetCursor(curs^^);
  131.                 hunlock(handle(curs));
  132.                 if releasecursor then
  133.                     ReleaseResource(handle(curs));
  134.             end
  135.         else
  136.             InitCursor;
  137.     end;
  138.  
  139.     procedure MCWatchCursor;
  140.     begin
  141.         SetCursor(GetCursor(watchCursor)^^);
  142.     end;
  143.  
  144.     procedure MCIBeamCursor;
  145.     begin
  146.         SetCursor(GetCursor(iBeamCursor)^^);
  147.     end;
  148.  
  149.     procedure MCPlusCursor;
  150.     begin
  151.         SetCursor(GetCursor(plusCursor)^^);
  152.     end;
  153.  
  154.     procedure MCCrossCursor;
  155.     begin
  156.         SetCursor(GetCursor(crossCursor)^^);
  157.     end;
  158.  
  159.     procedure MCArrowCursor;
  160.     begin
  161.         SetCursor(arrow);
  162.     end;
  163.  
  164.     procedure MCStartAnimCursor (acurresid: integer);
  165.         var
  166.             acurh: Handle;
  167.             tmplong: longint;
  168.             tmpint: integer;
  169.             ignore: integer;
  170.             i: integer;
  171.     begin
  172. {MCHasColourQD returns true if the machine has }
  173. {Colour Quickdraw, and therefore supports colour}
  174.         gMCCurrHasColour := MCHasColourQD;
  175.  
  176.         MCcursCursorsOn := true;
  177.         MCcursLastTime := TickCount;
  178.         gMCcurracurid := 0;
  179.         acurh := nil;
  180.         acurh := Get1Resource('acur', acurresid);
  181.         if (acurh = nil) or (ResError <> noErr) then
  182.             exit(MCStartAnimCursor);
  183.         gMCcurracurid := acurresid;
  184.         hlock(acurh);
  185. {Get the number of frames}
  186.         BlockMove(acurh^, @gMCcurscount, 2);
  187. {Get the frame dah dah dah}
  188.         BlockMove(pointer(ord4(acurh^) + 2), @MCcursInterval, 2);
  189. {Get the ids}
  190.         for i := 1 to gMCcurscount do
  191.             begin
  192.                 BlockMove(pointer(ord4(acurh^) + 2 + 2 + (4 * (i - 1))), @tmplong, 4);
  193.                 gMCacurids[i] := HiWord(tmplong);
  194.             end;
  195.         hunlock(acurh);
  196.         ReleaseResource(acurh);
  197.     end;
  198.  
  199.     procedure MCNextAnimCursor;
  200.     begin
  201.         if MCcursCursorsOn = false then
  202.             exit(MCNextAnimCursor);
  203.         if TickCount > MCcursLastTime + MCcursInterval then
  204.             begin
  205.                 MCcursLastTime := TickCount;
  206.                 if gMCcurracurid = 0 then
  207.                     begin
  208.                         MCWatchCursor;
  209.                         exit(MCNextAnimCursor);
  210.                     end;
  211.                 if gMCcurrcurindex = gMCcurscount then
  212.                     gMCcurrcurindex := 1
  213.                 else
  214.                     gMCcurrcurindex := gMCcurrcurindex + 1;
  215.                 MCSetCursor(gMCacurids[gMCcurrcurindex], false); {don't release the cursor}
  216.             end;
  217.     end;
  218.  
  219.     procedure MCResetAnimCursor;
  220.         var
  221.             curs: CursHandle;
  222.             ccurs: CCrsrHandle;
  223.             i, resid: integer;
  224.     begin
  225.         for i := 1 to kMCCurMaxCursors do
  226.             begin
  227.                 resid := gMCacurids[i];
  228.                 if gMCCurrHasColour then
  229.                     begin
  230.                         ccurs := GetCCursor(resid);
  231.                         if (ccurs <> nil) and (ResError = noErr) then
  232.                             DisposeCCursor(ccurs);
  233.                     end;
  234.                 curs := GetCursor(resid);
  235.                 if (curs <> nil) and (ResError = noErr) then
  236.                     ReleaseResource(handle(curs));
  237.                 gMCacurids[i] := 0;
  238.             end;
  239.         gMCcurrcurindex := 0;
  240.         gMCcurracurid := 0;
  241.         MCcursCursorsOn := false;
  242.     end;
  243. end.